package com.ssm.design.strategy;

import com.ssm.design.common.dto.OrderDto;
import com.ssm.design.common.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

@Component
public class OrderFactory {

    private Map<OrderTypeEnum, OrderService> orderServiceMap = new HashMap<>();
    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void initOrderServiceMap() {
        Map<String, OrderService> orderServiceMap =
                applicationContext.getBeansOfType(OrderService.class);
        orderServiceMap.forEach((k, v)
                -> this.orderServiceMap.put(v.getOrderType(), v));
    }

    public void createOrder(OrderDto orderDto) {
        orderServiceMap.get(orderDto.getOrderTypeEnum()).createOrder();
    }

}
